<?php
/*
 * 主要功能:
 *
 * dreamxyp(QQ:31996798) - PHP - class.tag.php
 * coding:夏一平
 * 創建時間:2006-11-3
 */
 class tag
 {
 	/*
 	 * 功能:格式化標識
 	 */
 	static public function format($tag)
 	{
 		$qj2bj = array(
			'１' => '1', '２' => '2','３' => '3', '４' => '4', '５' => '5',
			'６' => '6', '７' => '7', '８' => '8', '９' => '9', '０' => '0',
			'ａ' => 'a', 'ｂ' => 'b', 'ｃ' => 'c', 'ｄ' => 'd', 'ｅ' => 'e',
			'ｆ' => 'f', 'ｇ' => 'g', 'ｈ' => 'h', 'ｉ' => 'i', 'ｊ' => 'j',
			'ｋ' => 'k', 'ｌ' => 'l', 'ｍ' => 'm', 'ｎ' => 'n', 'ｏ' => 'o',
			'ｐ' => 'p', 'ｑ' => 'q', 'ｒ' => 'r', 'ｓ' => 's', 'ｔ' => 't',
			'ｕ' => 'u', 'ｖ' => 'v', 'ｗ' => 'w', 'ｘ' => 'x', 'ｙ' => 'y',
			'ｚ' => 'z', 'Ａ' => 'A', 'Ｂ' => 'B', 'Ｃ' => 'C', 'Ｄ' => 'D',
			'Ｅ' => 'E', 'Ｆ' => 'F', 'Ｇ' => 'G', 'Ｈ' => 'H', 'Ｉ' => 'I',
			'Ｊ' => 'J', 'Ｋ' => 'K', 'Ｌ' => 'L', 'Ｍ' => 'M', 'Ｎ' => 'N',
			'Ｏ' => 'O', 'Ｐ' => 'P', 'Ｑ' => 'Q', 'Ｒ' => 'R', 'Ｓ' => 'S',
			'Ｔ' => 'T', 'Ｕ' => 'U', 'Ｖ' => 'V', 'Ｗ' => 'W', 'Ｘ' => 'X',
			'Ｙ' => 'Y', 'Ｚ' => 'Z', '　' => ' ', '，' => ',', '。' => '.',
			'？' => '?', '＜' => '<', '＞' => '>', '〔' => '[', '〕' => ']',
			'＊' => '*', '＆' => '&', '' => '^', '％' => '%', '＃' => '#',
			'＠' => '@', '！' => '!', '（' => '(', '）' => ')', '＋' => '+',
			'－' => '-', '｜' => '|', '：' => ':', '；' => ':',
			'｛' => '{', '｝' => '}', '／' => '/', '〞' => '"'
	     );
		$tag 	= strtr($tag, $qj2bj);
		$delstr	= array(
			"0","1","2","3","4","5","6","7","8","9",
			"\"","，",  "'", "「","」","『","』","&",
			"?","~","^","《","》", ">", "<", "{","}",
			"[", "]","!","@","#","\$","￥","^","……",
			"*","(", ")","+","%");
		$tag	= str_replace($delstr, "", $tag);
 		$delstr	= array(" ","|","、",".","\\","/",":",";","¯","_","-");
		$tag	= str_replace($delstr, ",", $tag);
		$tag    = explode(",",$tag);
		foreach($tag as &$value)
		{
			if(strlen($value)>2)$tags[]= $value;
		}
		$tag	= array_unique($tags);
		$tag 	= implode(",",$tag);
		return $tag;
 	}
	/*
 	 * 功能:現顯出來
 	 */
	static public function show($array)
	{
		$rs = array();
		if(!is_array($array)) $array = explode(',',$array);
		foreach($array as &$v)
		{
			//$rs[] = '<a href="http://www.dreamxyp(QQ:31996798)/w/show_list.php?key='.urlencode($v).'" id="tag">'.$v.'</a>';
			$rs[] = '<a href="http://www.dreamxyp(QQ:31996798)/so/index?key='.urlencode($v).'" id="tag">'.$v.'</a>';
		}
		return $rs;
	}
 	/*
 	 * 功能:得到更新或插入數據庫數組
 	 */
 	static public function toSqlArray($tag,$id=0)
 	{
 		$rs = array();
 		$i  = 0;
 		if(!is_array($tag))
 		{
 			$tag = explode(',',self::format($tag));
 		}
 		if($id)
 		{
	 		foreach($tag as $v)
	 		{
	 			if($v and $i<5)$rs[] = array($id=>$v);
	 			$i++;
	 		}
 		}else
 		{
 			foreach($tag as $v)
	 		{
	 			if($v and $i<5)$rs['tags'.$i] = $v;
	 			$i++;
	 		}
 		}
 		return $rs;
 	}
 	/*
 	 * 功能:刪除
 	 */
 	static public function del($id)
	{
		global $db;
		$sql = array(
			'tags0'=>'','tags1'=>'','tags2'=>'',
			'tags3'=>'','tags4'=>''
		);
		$db->delete('flash_tag',' id = ? ',$id);
		$db->update('flash_flv_date',$sql,' id = ? ',$id);
	}
	/*
	 * 功能:添加
	 */
	static public function add($id,$str)
	{
		global $db;
		self::del($id);
		$sql = self::toSqlArray($str);
		$db->update('flash_flv_date',$sql,' id = ? ',$id);
		$sql = self::toSqlArray($str,$id);
		foreach($sql as $v)
		{
			$db->insert('flash_tag',$v);
		}
	}
	/*
	 * 功能:更新
	 */
 	static public function up($id,$str) 
 	{
 		self::add($id,$str);
 	}
 }
?>
